﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
using System.IO;

namespace ChamCongMain.Form
{
    public partial class frmCongNhan : DevExpress.XtraEditors.XtraForm
    {
        CongNhan cn = new CongNhan();
       public  Delegates.RefeshGrid LoadData;
        ChucNang chucnang;
       
        public frmCongNhan(ChucNang chucnang, CongNhan congnhan)
        {
            InitializeComponent();
            TinhThanhBLL ttbll = new TinhThanhBLL();
            glkNoiCap.Properties.DataSource = ttbll.GetAllTinhThanh();
            glkTinhThanh.Properties.DataSource = ttbll.GetAllTinhThanh();
            this.chucnang = chucnang;
            if (chucnang == ChucNang.Them)
            {
                this.Text = "Thêm Công Nhân";
            }
            else
            {
                this.Text = "Sửa Công Nhân";
                this.cn = congnhan;
                SetValueForForm();

            }
        }

        CongNhan GetValueFromForm()
        {
            if (pcCongNhan.Image != null)
            {
                Image a = pcCongNhan.Image;
                MemoryStream ms = new MemoryStream();
                a.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] img = ms.ToArray();
                cn.Anh = img;
            }
            cn.HoTen = txtHoTen.Text;
            cn.GioiTinh = cboGioiTinh.Text;
            if (dateNgaySinh.EditValue != null)
                cn.NgaySinh = (DateTime)dateNgaySinh.EditValue;
            cn.DiaChi = txtDiaChi.Text;
            if (glkTinhThanh.EditValue != null)
                cn.TinhThanhId = (long)glkTinhThanh.EditValue;
            cn.Sdt = txtSDT.Text;
            cn.CMMD = txtSoCMND.Text;
            if (dateNgayCapCMND.EditValue != null)

                cn.NgayCapCMND = (DateTime)dateNgayCapCMND.EditValue;
            if (glkNoiCap.EditValue != null)
                cn.NoiCapCMND = (long)glkNoiCap.EditValue;
            cn.GhiChu = txtGhiChu.Text;
            return cn;
        }
        void SetValueForForm()
        {

            if (cn.Anh != null)
            {
                MemoryStream ms = new MemoryStream((byte[])cn.Anh.ToArray());
                pcCongNhan.Image = Image.FromStream(ms);
                pcCongNhan.Refresh();
            }
            txtHoTen.Text = cn.HoTen;
            cboGioiTinh.Text = cn.GioiTinh;
            txtDiaChi.Text = cn.DiaChi;
            txtSoCMND.Text = cn.CMMD;
            if (cn.NgaySinh != null)
                dateNgaySinh.EditValue = cn.NgaySinh;
            txtSDT.Text = cn.Sdt;
            if (cn.NgayCapCMND != null)
                dateNgayCapCMND.EditValue = cn.NgayCapCMND;
            if (cn.NoiCapCMND != null)
                glkNoiCap.EditValue = cn.NoiCapCMND;
            if (cn.TinhThanhId != null)
                glkTinhThanh.EditValue = cn.TinhThanhId;
            txtGhiChu.Text = cn.GhiChu;
        }
        void ClearForm()
        {
            pcCongNhan.Image = null;
            txtHoTen.Text = string.Empty;
            cboGioiTinh.Text = string.Empty;
            txtDiaChi.Text = string.Empty;
            txtSoCMND.Text = string.Empty;
            dateNgaySinh.EditValue = string.Empty;
            txtSDT.Text = string.Empty;
            dateNgayCapCMND.EditValue = string.Empty;
            glkNoiCap.EditValue = string.Empty;
            glkTinhThanh.EditValue = string.Empty;
            txtGhiChu.Text = string.Empty;
            chucnang = ChucNang.Them;
            cn = null;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            CongNhan cn = GetValueFromForm();
            CongNhanBLL cnbll = new CongNhanBLL();
            if (chucnang == ChucNang.Them)
            {
                cnbll.InsertCongNhan(cn);
                XtraMessageBox.Show("Thêm thành công", "Thêm", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                cnbll.UpdateCongNhan(cn);
                XtraMessageBox.Show("Sửa thành công", "Sửa", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            LoadData(cnbll.GetAllCongNhan());
            this.Close();
        }



        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            CongNhan cn = GetValueFromForm();
            CongNhanBLL cnbll = new CongNhanBLL();
            if (chucnang == ChucNang.Them)
            {
                cnbll.InsertCongNhan(cn);
                XtraMessageBox.Show("Thêm thành công", "Thêm", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                cnbll.UpdateCongNhan(cn);
                XtraMessageBox.Show("Sửa thành công", "Sửa", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            LoadData(cnbll.GetAllCongNhan());
            ClearForm();
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Bạn có muốn thoát không?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }
    }
}